package com.cskaoyan.config.timer;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponExample;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.GoodsMapper;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 定时查询改变数据库优惠卷状态，查看是否过期
 */

public class UpdateCouponStatusTask extends QuartzJobBean {

    @Autowired
    CouponMapper couponMapper;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        //查询出所有过期的优惠卷
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andEndTimeLessThan(new Date()).andStatusEqualTo(0);
        List<Coupon> coupons = couponMapper.selectByExample(example);
        //如果不为null,则进行以下
        if (coupons != null && coupons.size() > 0) {
            //遍历改变优惠卷状态
            //记录id
            List<Integer> list = new ArrayList<>();
            for (Coupon coupon : coupons) {
                System.out.println(coupon);
                list.add(coupon.getId());
            }
            //再次创建条件，一次性改变效率更高
            CouponExample updateExample = new CouponExample();
            CouponExample.Criteria updateCriteria = updateExample.createCriteria();
            updateCriteria.andIdIn(list);
            Coupon coupon = new Coupon();
            coupon.setStatus(1);
            coupon.setUpdateTime(new Date());
            //执行
            // couponMapper.up
            couponMapper.updateByExampleSelective(coupon, updateExample);
        }
    }
}

